package cn.com.pajx.pajx_visitor_v3.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pajx.facelibrary.utils.DensityUtil;
import com.rcw.swiperefreshrecyclerview.BaseAdapter;
import com.rcw.swiperefreshrecyclerview.ViewHolder;

import java.util.List;

import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.bean.VisiteBean;
import cn.com.pajx.pajx_visitor_v3.ui.view.TextureOutlineProvider;
import cn.com.pajx.pajx_visitor_v3.utils.BaseImageUtils;
import cn.com.pajx.pajx_visitor_v3.utils.CornerTransform;
import cn.com.pajx.pajx_visitor_v3.utils.DateUtil;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;

/**
 * Created by ruancw on 2024/2/28.
 * Description: pajx_visitor
 */
public class VisiteAdapter extends BaseAdapter<VisiteBean> {

    private OnFunctionListener onFunctionListener;
    private String visite_status;

    public VisiteAdapter(Context context, int layoutId, List<VisiteBean> beans) {
        super(context, layoutId, beans);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ViewHolder holder, VisiteBean bean, int position) {
        ImageView ivPhoto = holder.getView(R.id.iv_photo);
        TextView tvOrderTime = holder.getView(R.id.tv_order_time);
        TextView tvStatus = holder.getView(R.id.tv_status);
        TextView tvVisitor = holder.getView(R.id.tv_visitor);
        TextView tvFunction = holder.getView(R.id.tv_function);
        TextView tvPhone = holder.getView(R.id.tv_phone);
        TextView tvIDCard = holder.getView(R.id.tv_card_no);
        TextView tvInterviewee = holder.getView(R.id.tv_name);
        TextView tvCause = holder.getView(R.id.tv_cause);
        //Glide设置圆角图片
        CornerTransform transform = new CornerTransform(context, DensityUtil.dp2px(context, 16));
        transform.setCornerRadius(false, false, false, false);
        if (TextUtils.isEmpty(bean.getVis_photo())) {
            BaseImageUtils.setLocalCornerImage(context, ivPhoto, R.mipmap.icon_avatar_big, transform);
        } else {
            BaseImageUtils.setNetCornerImage(context, ivPhoto, bean.getVis_photo(), R.mipmap.icon_avatar_big, transform);
        }
        ivPhoto.setOutlineProvider(new TextureOutlineProvider(DensityUtil.dp2px(context, 16)));
        ivPhoto.setClipToOutline(true);
        tvOrderTime.setText(DateUtil.getAllTime(bean.getCreate_time()));
        tvVisitor.setText(TextUtils.isEmpty(bean.getVis_name_des()) ? bean.getVis_name() : bean.getVis_name_des());
        tvInterviewee.setText(TextUtils.isEmpty(bean.getUser_name_des()) ? bean.getUser_name() : bean.getUser_name_des());
        tvFunction.setText("开始拜访");
        tvPhone.setText(bean.getVis_phone_des());
        tvIDCard.setVisibility(TextUtils.isEmpty(bean.getVis_idcard_des()) ? View.INVISIBLE : View.VISIBLE);
        tvIDCard.setText(bean.getVis_idcard_des());
        tvCause.setText(bean.getVis_note());
        visite_status = bean.getVis_status();
        LogUtils.e("visite_status==="+visite_status);
        if (TextUtils.isEmpty(visite_status)) visite_status = "999";
        boolean isShowFunction = TextUtils.equals(visite_status, "1") || TextUtils.equals(visite_status, "3");
        tvFunction.setVisibility(isShowFunction ? View.VISIBLE : View.INVISIBLE);
        switch (visite_status) {
            case "0"://待审批
                tvStatus.setText("待审批");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorOrange));
                tvStatus.setBackgroundResource(R.drawable.shape_status_approve);
                break;
            case "1"://待拜访
                tvStatus.setText("待拜访");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorGreen));
                tvStatus.setBackgroundResource(R.drawable.shape_status_waiting);
                tvFunction.setBackgroundResource(R.drawable.shape_confirm_gradient);
                tvFunction.setText("开始拜访");
                break;
            case "2"://拒绝拜访
                tvStatus.setText("拒绝拜访");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorRed));
                tvStatus.setBackgroundResource(R.drawable.shape_status_reject);
                break;
            case "3"://拜访中
                tvStatus.setText("拜访中");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorBlue27));
                tvStatus.setBackgroundResource(R.drawable.shape_status_visiting);
                tvFunction.setBackgroundResource(R.drawable.shape_end_visite_gradient);
                tvFunction.setText("结束拜访");
                break;
            case "4"://结束拜访
                tvStatus.setText("拜访结束");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorOrange22));
                tvStatus.setBackgroundResource(R.drawable.shape_status_finish);
                break;
            default://已失效
                tvStatus.setText("已失效");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorGrey90));
                tvStatus.setBackgroundResource(R.drawable.shape_status_invalid);
                break;
        }
        tvFunction.setOnClickListener(view -> {
            if (onFunctionListener != null) {
                onFunctionListener.onFunction(bean);
            }
        });
    }

    public void setOnFunctionListener(OnFunctionListener onFunctionListener) {
        this.onFunctionListener = onFunctionListener;
    }

    public interface OnFunctionListener {
        void onFunction(VisiteBean bean);
    }
}
